<?php

namespace App\Services\Code;

use App\Models\User;
use App\Notifications\CodeNotification;
use Auth;
use SmsService;
use UserService;

/**
 * 验证码服务
 * @package App\Services
 */
class CodeService
{
    protected $email;




    public function send($email)
    {
        $this->email = $email;
        $user = User::where('email', $email)->first();
        $user->notify(new CodeNotification($this->code()));
    }

    /**
     * 验证码校对
     *
     * @param string $account
     * @param string $code
     * @return void
     */
    public function check(string $account, string $code)
    {
        return cache($account) == $code;
    }

    /**
     * 生成验证码
     *
     * @return void
     */
    protected function code()
    {
        $code = mt_rand(1000, 9999);
        cache([$this->email => $code], now()->addMinutes(10));
        return $code;
    }
}
